package com.example.treehole.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.CircularProgressDrawable;

import com.example.treehole.R;
import com.example.treehole.viewobject.DynamicComment;
import com.example.treehole.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class DynamicCommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<DynamicComment> data;

    public void setData(List<DynamicComment> data) {
        this.data = data;
    }

    public DynamicCommentAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public DynamicCommentAdapter(Context context, List<DynamicComment> data) {
        this.mContext = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        // 1.有数据  其他无数据
        if (viewType == 1) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic_comment, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dynamic_comment_empty, parent, false);
        }
        return new ViewHolder(view);
    }


    @Override
    // 通过getItemViewType的返回值来选择具体的item显示
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder vh= (ViewHolder) holder;
        vh.tvContent.setText(data.get(position).getComment());
        vh.tvCommenter.setText(data.get(position).getUserName());
        vh.tvTime.setText(data.get(position).getTvTime());
        Picasso.get().load(data.get(position).getUserHeader()).placeholder(mContext.getResources().getDrawable(R.mipmap.tree_hole)).into(vh.ivHeader);
    }

    @Override
    public int getItemViewType(int position) {
        if (data == null || data.size() == 0)
            return 2;
        return 1;
    }


    @Override
    public int getItemCount() {
        if (data == null || data.size() == 0) {
            return 0;
        } else {
            return data.size();
        }
    }


    //接口
    public interface OnItemClickListener {
        void onClick(int pos);
    }

    private OnItemClickListener listener;

    public void setOnMyItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_header)
        CircleImageView ivHeader;
        @BindView(R.id.tv_commenter)
        TextView tvCommenter;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_time)
        TextView tvTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}


